package org.jcut.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.jcut.bean.Shopcar;
import org.jcut.data.Data;
import org.jcut.service.IShopcarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@RestController
@RequestMapping("/shopcar")
public class ShopCarController {
	@Autowired
	private Data data;
	@Autowired
	private IShopcarService service;
	/**
	 * @author 胡琪
	 *  /**
     * 
     *
     * @param pageNum  页码
     * @param pageSize 每页显示数量
     * String uId 用户id（必填）
	 * 
	 * */
	@RequestMapping(value="/select")
	public Object select(@RequestParam("uId")String uId,@RequestParam(value="pageNum",required = false)String pageNum,@RequestParam(value="pageSize",required = false)String pageSize) {
		if((pageNum!=null&&!("".equals(pageNum)))&&(pageSize!=null&&!("".equals(pageSize)))) {
			PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
		}
		List<Shopcar> list = service.selectByUid(uId);
		PageInfo<Shopcar> pageInfo = new PageInfo<>(list);
		data.setStatus("1");
		data.setMsg("查询成功！");
		data.setData(pageInfo);
		return data;
	}
	/**
	 *@author 胡琪 
	 * String uId 用户id
	 * String cId 商品id 
	 * String cImg 商品展示图片的名字
	 * String cName 商品名称
	 * String cType 商品类型     
	 * String cSpecs 商品包装
	 * String cPrice 商品单价
	 * String cNum   购买数量
	 * String sPrice  商品总价
	 * 
	 * */
	@RequestMapping(value="/add",params = {"sPrice","cPrice","cNum","cSpecs","cType","uId","cId","cImg","cName"})
	public Object add(Shopcar shop) {
		shop.setId(UUID.randomUUID().toString());
		shop.setTime(new Date());
		shop.setDis(Integer.parseInt("1"));
		int i=service.insert(shop);
		if(i>0) {
			data.setData(i);
			data.setMsg("添加成功！");
			data.setStatus("1");
		}else {
			data.setData(i);
			data.setMsg("服务器异常！");
			data.setStatus("0");
		}
		return data;
	}
	/**
	 *@author 胡琪 
	 * String uId 用户id
	 * String cId 商品id 
	 * String cImg 商品展示图片的名字
	 * String cName 商品名称
	 * String cType 商品类型     
	 * String cSpecs 商品包装
	 * String cPrice 商品单价
	 * String cNum   购买数量
	 * String sPrice  商品总价
	 * String id     购物车id
	 * */
	@RequestMapping(value="/update",params = {"id","sPrice","cPrice","cNum","cSpecs","cType","uId","cId","cImg","cName"})
	public Object update(Shopcar shop) {
		shop.setDis(Integer.parseInt("1"));
		int i=service.updateByPrimaryKey(shop);
		if(i>0) {
			data.setData(i);
			data.setMsg("修改成功！");
			data.setStatus("1");
		}else {
			data.setData(i);
			data.setMsg("服务器异常！");
			data.setStatus("0");
		}
		return data;
	}
	
	/**
	 * @author 胡琪
	 * String id 购物车id
	 * 
	 * */
	@RequestMapping(value = "/delete",params = "id")
	public Object delete(String id) {
		int i=service.deleteByPrimaryKey(id);
		if(i>0) {
			data.setData(i);
			data.setMsg("删除成功！");
			data.setStatus("1");
		}else {
			data.setData(i);
			data.setMsg("服务器异常！");
			data.setStatus("0");
		}
		return data;
	}
	@RequestMapping("/userSum")
	public Object UserSum(@RequestParam(value="uId",required = true)String uId) {
		long count=service.UserSum(uId);
		data.setData(count);
		data.setMsg("查询成功");
		data.setStatus("1");
		return data;
	}
	/**
	 * @author 胡琪
	 * @param cId 商品id
	 * @param uId 用户id
	 * @param cNum 商品数量
	 * @param sPrice 商品总价
	 * */
	@RequestMapping("/addNum")
	public Object addNum(Shopcar shopcar) {
		int i=service.addNum(shopcar);
		if(i>0) {
			data.setData(i);
			data.setMsg("添加成功");
			data.setStatus("1");
		}else {
			data.setData(i);
			data.setMsg("服务器异常");
			data.setStatus("0");
		}
		return data;
	}
	/**
	 * @author 胡琪
	 * @param cId 商品id
	 * @param uId 用户id
	 * 
	 * */
	@RequestMapping("/getNum")
	public Object getNum(Shopcar shopcar) {
		long num=service.getNum(shopcar);
		if(num>0) {
			data.setData(num);
			data.setStatus("0");
			data.setMsg("已经添加过");
		}else {
			data.setData(num);
			data.setStatus("1");
			data.setMsg("未添加过");
		}
		return data;
	}
	@RequestMapping("/cutNum")
	public Object cutNum(Shopcar shopcar) {
		int i=service.cutNum(shopcar);
		if(i>0) {
			data.setData(i);
			data.setMsg("成功！");
			data.setStatus("1");
		}else {
			data.setData(i);
			data.setMsg("服务器异常");
			data.setStatus("0");
		}
		return data;
	}
	@RequestMapping("/selectByIn")
	public Object selectPostIn(String cIds,String uId) {
		String[] arr=cIds.split(",");
		System.err.println(cIds);
		System.out.println(uId);
		List<Object> item=new ArrayList<Object>();
		
		for(String a:arr) {
			item.add(a);
		}
		List<Shopcar> list=new ArrayList<Shopcar>();
		List<Shopcar> myList=service.selectPostIn(item);
		for(Shopcar my:myList) {
			if(my.getuId().equals(uId)) {
				list.add(my);
			}
		}
		data.setData(list);
		data.setMsg("查询成功");
		data.setStatus("1");
		return data;
	}
}
